package com.qts.crm.util;

import com.alibaba.fastjson.JSON;
import com.qts.crm.dto.ResultSupport;
import com.squareup.okhttp.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;

/**
 * http调用
 * Created by qucheng on 17/7/13.
 */
public class OkHttpUtils {

    private static Logger logger = LoggerFactory.getLogger(OkHttpUtils.class);

    public static ResultSupport postRequest(String url, Object reqBody) throws IOException {
        MediaType jsonType = MediaType.parse("application/json; charset=utf-8");
        //创建一个OkHttpClient对象
        OkHttpClient okHttpClient = new OkHttpClient();
        RequestBody requestBody = RequestBody.create(jsonType, JSON.toJSONString(reqBody));
        Request request = new Request.Builder().url(url).post(requestBody).build();
        Call call = okHttpClient.newCall(request);
        try {
            Response response = call.execute();
            String jsonResult = response.body().string();
            return JSON.parseObject(jsonResult, ResultSupport.class);
        } catch (IOException e) {
            logger.error("http请求失败");
            throw e;
        }
    }
}
